<% page_title "Webhooks" %>

<%= render 'projects/header', project: @project, tab: "webhooks" %>

<% inbound_webhooks = @project.webhooks %>
<% outbound_webhooks = @project.outbound_webhooks %>

<section class="tabs">
  <h2>Inbound Webhooks</h2>
  <p>In order to automatically trigger a deploy to a stage, add a branch &rarr; stage mapping below
  and paste in the project's webhook URL to your CI service of choice.</p>

  <dl>
    <% Samson::Integration::SOURCES.each do |source| %>
      <dt>
        <%= source.titleize %>:</dt><dd><%= send("integrations_#{source}_deploy_url", @project.token) %>
        <% if source == 'github' && token = Integrations::GithubController.secret_token %>
          <br/>
          <% if current_user.admin_for?(@project) %>
            (Set <%= link_to "secret token", "javascript:prompt('Copy this token', '#{token}')" %> in <%= link_to 'hook settings', "https://github.com/#{@project.repository_path}/settings/hooks" %>)
          <% else %>
            (Secret token required, ask a project admin)
          <% end %>
        <% end %>
      </dd>
    <% end %>
  </dl>

  <% if inbound_webhooks.any? %>
    <h2>Webhooks mappings</h2>
    <ul>
      <% inbound_webhooks.each do |webhook| %>
        <li>
          <strong><%= webhook.branch.blank? ? 'Any' : webhook.branch %></strong>
          branch &rarr;
          <strong><%= webhook.stage&.name || "Deleted stage" %></strong>
          stage for:
          <%= webhook.source.humanize %>
          (<%= link_to "remove", [@project, webhook], method: :delete %>)
        </li>
      <% end %>
    </ul>
  <% end %>

  <h2>Add webhook mapping</h2>
  <%= form_for :webhook, url: project_webhooks_path(@project), html: { class: "form-inline" } do |form| %>
    <div class="form-group">
      <%= form.text_field :branch, class: "form-control", placeholder: "Branch (Blank for any)" %>
    </div>

    &rarr;

    <div class="form-group">
      <%= form.collection_select :stage_id, @project.stages, :id, :name, {}, class: "form-control" %>
    </div>

    <div class="form-group">
      <%= form.select :source, webhook_sources_for_select(Samson::Integration::SOURCES), {}, class: "form-control" %>
    </div>

    <p></p>

    <%= form.submit "Add webhook", class: "btn btn-primary" %>
  <% end %>

  <% if hook = WebhookRecorder.read(@project) %>
    <h2>Latest webhook</h2>
    <table class="table" id="latest_response">
      <tr><th>Arrived</th><td><%= hook.fetch(:time) %></td></tr>
      <tr>
        <th>Request</th>
        <td>
          <% if current_user.admin_for?(current_project) %>
            <pre><%= hook.fetch(:request_headers).map { |k,v| "#{k}: #{v}" }.join("\n") %></pre>
            Raw params:
            <pre><%= JSON.pretty_generate(hook.fetch(:request_params)) %></pre>
          <% else %>
            ... visible for admins ...
          <% end %>
        </td>
      </tr>
      <tr>
        <th>Response</th>
        <td>
          Status: <%= hook.fetch(:response_code) %><br/>
          Body:
          <% body = hook.fetch(:response_body) %>
          <pre><%= body.start_with?('{') ? (JSON.pretty_generate(JSON.parse(body)) rescue body) : body %></pre>
        </td>
      </tr>
      <tr>
        <th>Log</th>
        <td>
          <pre><%= hook.fetch(:log) %></pre>
        </td>
      </tr>
    </table>
  <% end %>
  <h2></h2>

  <h2>Outbound Webhooks</h2>
  <p>Outbound webhooks POST to an external url after a deploy has finished. The request body will contain deploy, stage, and project information.</p>
  <% if @project.deploys.last %>
    <p>
      <%= link_to 'Example payload', '#', class: 'toggle', data: {target: '#payload-sample'} %>
    </p>
    <% example = JSON.pretty_generate(OutboundWebhook.deploy_as_json(@project.deploys.last)) %>
    <pre id='payload-sample' style='display:none'><%= example %></pre>
  <% else %>
    <p>Deploy this project at least once to see a sample payload.</p>
  <% end %>

  <% if outbound_webhooks.any? %>
    <h2>Webhooks mappings</h2>
    <ul>
      <% outbound_webhooks.each do |outbound_webhook| %>
        <li>
          POST to
          <strong><%= outbound_webhook.url %></strong>
          on the
          <strong><%= outbound_webhook.stage.name %></strong>
          stage
          <strong>
          <% if outbound_webhook.username.present? %>
            <%= "with Basic Auth as #{outbound_webhook.username}" %>
          <% end %>
          </strong>
          (<%= link_to "remove", [@project, outbound_webhook], method: :delete %>)
        </li>
      <% end %>
    </ul>
  <% end %>

  <h2>Add an outbound webhook</h2>
  <%= form_for [:project, (@new_outbound_webhook || OutboundWebhook.new)], html: { class: 'form-inline' } do |form| %>
    <div class="form-group">
      <%= form.text_field :url, class: "form-control", placeholder: "Full Url" %>
    </div>
    <div class="form-group">
      <%= form.collection_select :stage_id, @project.stages, :id, :name, {}, class: "form-control" %>
    </div>
    <div class="form-group">
      <%= form.text_field :username, class: "form-control", placeholder: "Username (optional)" %>
    </div>
    <div class="form-group">
      <%= form.text_field :password, class: "form-control", placeholder: "Password (optional)", type: "password" %>
    </div>
    <p></p>
    <div class="form-group">
      <%= form.submit "Add Outbound Webhook", class: "btn btn-primary" %>
    </div>
  <% end %>
</section>
